package fr.pud.client;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;

import org.openstreetmap.gui.jmapviewer.DefaultMapController;

import com.apple.eawt.Application;

import fr.pud.client.controller.Controller;
import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.PUDStationDatabase;
import fr.pud.utils.Tools;
import fr.pud.utils.Tools4PUDStation;

public class LaunchProgram {
    public static void changeMenuBar() {
        if (DefaultMapController.isPlatformOsx()) {
            System.setProperty(
                    "com.apple.mrj.application.apple.menu.about.name",
                    LanguagesConstants.LANGUAGE
                            .getString(LanguagesConstants.TITLE));
            System.setProperty("apple.laf.useScreenMenuBar", "true");
            Application application = Application.getApplication();
            Image imageDock = null;
            try {
                imageDock = Toolkit.getDefaultToolkit().getImage(
                        LaunchProgram.class.getResource("images/osmIcon.png"));
            }
            catch (Exception e) {
                Tools.writeToLogFile(
                        "osmIcon.png "
                                + LanguagesConstants.LANGUAGE
                                        .getString(LanguagesConstants.ERROR_FILE),
                        false);
            }
            application.setDockIconImage(imageDock);
        }
    }

    @SuppressWarnings("unused")
    public static void main(String[] args) throws IOException {
        changeMenuBar();
        System.setProperty("file.encoding", "UTF-8");
        if (Tools.isConnected()) {
            Tools.initLogFile();
            // Load the database of the addresses
            PUDStationDatabase.getInstance();
            if (DefaultMapController.isPlatformOsx()) {
                // If the user uses cmd + Q to quit the programm, this operation
                // will be not execute.
                // So we execute this method at the start of the program only
                // for mac Os X
                Tools4PUDStation.serializePUDStationsDatabase();
            }
            ImagesDatabase loadImages = new ImagesDatabase();
            /*
             * Initialisation of the model
             */
            Controller launch = new Controller();
        }
    }
}
